
function CNAcheckLogs () {
    top.document.title = "Pardus ";
    var snoozed = false;
    var snoozeUntil = localStorage.getItem ("po_cna_snooze_until");
    if (snoozeUntil) {
        var su = Number (snoozeUntil);
        var now = Math.floor ((new Date()).getTime () / 1000);
        if (now > su) {
            // Check if Now is higher than the Snooze limit if so,
            // clear it and let the script check for alerts.
            localStorage.removeItem ("po_cna_snooze_until");
        } else {
            // If we're still in snooze period so let's create the Un-snooze link and skip alert checking
            snoozed = true;
        }
    }
    
    var image = document.getElementsByTagName("img");
    var pm = false;
    var am = false;
    var cl = false;
    var alert = false;
    for(i=0;i<image.length;i++){
        if(image[i].getAttribute("title")=="New private messages"){
            if (!snoozed) top.CNAalertPM ();
            top.document.title += "-PM";
            pm = true;
            alert = true;
        }
        if(image[i].getAttribute("title")=="New alliance messages"){
            if (!snoozed) top.CNAalertAM ();
            top.document.title += "-AM";
            am = true;
            alert = true;
        }
        if(image[i].getAttribute("title")=="New combat log entries"){
            if (!snoozed) top.CNAalertCL ();
            top.document.title += "-CL";
            cl = true;
            alert = true;
        }
    }
    
    if (document.body.textContent.indexOf ("New alliance command station log entries!") != -1) {
        if (!snoozed) top.CNAalertACS ();
        top.document.title += "-ACS";
        alert = true;
    } else {
        top.CNAstopACS ();
    }
    
    if (!pm) top.CNAstopPM ();
    if (!am) top.CNAstopAM ();
    if (!cl) top.CNAstopCL ();
    
    if (alert) {
        // Install the Snooze link
        for (var i=0; i<document.links.length; i++) {
            if (document.links[i].textContent == "Rules") {
                var link = document.createElement ("div");
                if (snoozed) link.innerHTML = "<span> | <a id='po_cna_snooze' href='javascript:CNAUnSnooze();void(0);' style='color:lime'>Un-snooze</a></span>";
                else link.innerHTML = "<span> | <a id='po_cna_snooze' href='javascript:CNASnooze();void(0);' style='color:lime'>Snooze alert for 5min.</a></span>";
                document.links[i].parentNode.appendChild (link.firstChild);
                break;
            }
        }   
    }
}

function CNAUnSnooze () {
    var link = document.getElementById ("po_cna_snooze");
    if (link) {
        link.textContent = "Snooze alert for 5min.";
        link.setAttribute ("href", "javascript:CNASnooze();void(0);");
    }
    localStorage.removeItem ("po_cna_snooze_until");
    window.location.reload();
}

function CNASnooze() {
    try {
        top.CNAstopPM ();
        top.CNAstopAM ();
        top.CNAstopCL ();
        top.CNAstopACS ();
    } catch (e) {}
    var now = Math.floor ((new Date()).getTime () / 1000);
    var snooze_until = now + (5 * 60);
    localStorage.setItem ("po_cna_snooze_until", snooze_until);
    
    var link = document.getElementById ("po_cna_snooze");
    if (link) {
        link.textContent = "Un-snooze";
        link.setAttribute ("href", "javascript:CNAUnSnooze();void(0);");
    }
}

window.onload = function() {setTimeout (CNAcheckLogs, 500)};